<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getShop">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>


		<!--列表-->
		<el-table :data="shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="名称" width="120" sortable>
			</el-table-column>
			<el-table-column prop="tel" label="电话" width="180" sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="注册时间" width="180" sortable>
			</el-table-column>
			<el-table-column prop="address" label="地址" width="180" sortable>
			</el-table-column>
			<el-table-column prop="logo" label="logo" width="100" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" sortable>
				<template scope="scope">
					<span style="color: green" v-if="scope.row.state==2">已审核</span>
					<span style="color: red" v-if="scope.row.state==1">待审核</span>
					<span style="color: yellow" v-if="scope.row.state==0">待提交</span>
				</template>
			</el-table-column>
			<el-table-column prop="admin.username" label="店铺管理员" width="100" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" :current-page="page" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title=title :visible.sync="shopFormVisible" :close-on-click-modal="false">
			<el-form :model="shopForm" label-width="80px" :rules="shopFormRules" ref="shopForm">
				<el-form-item label="名称" prop="name">
					<el-input v-model="shopForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="tel">
					<el-input v-model="shopForm.tel" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="地址" prop="address">
					<el-input v-model="shopForm.address" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="logo" prop="logo">
					<el-input v-model="shopForm.logo" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="shopForm.state">
						<el-radio class="radio" :label="2">已审核</el-radio>
						<el-radio class="radio" :label="1">待审核</el-radio>
						<el-radio class="radio" :label="0">待提交</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="管理员" prop="admin">
					<!--<el-input v-model="shopForm.department.name" auto-complete="off"></el-input>-->
					<el-select v-model="shopForm.admin" placeholder="请选择" value-key="id">
						<el-option
								v-for="item in admins"
								:key="item.value"
								:label="item.username"
								:value="item">
							<span style="float: left">{{ item.username }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.id }}</span>
						</el-option>
					</el-select>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="shopFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				shops: [],
				total: 0,
				page: 1,
				pageSize:10,
				listLoading: false,
				sels: [],//列表选中列
				title:'',
				shopFormVisible: false,//编辑界面是否显示
				editLoading: false,
				shopFormRules: {
					name: [
						{required: true, message: '请输入姓名', trigger: 'blur'}
					],
					password: [
						{required: true, message: '请输入密码', trigger: 'blur'}
					]
				},
				//编辑界面数据
				shopForm: {
					id: null,
					name: '',
					tel: '',
					state: 1,
					registerTime: '',
					logo:'',
					address:'',
					admin:{}
				},
				admins:[]
			}
		},
		methods: {
			//获取当前页
			handleCurrentChange(val) {
				this.page = val;
				this.getShop();
			},
			//获取用户列表
			getShop() {
				let para = {
					localPage: this.page,
					name: this.filters.name
				};
				this.listLoading = true;
				this.$http.post("/shop", para)
						.then(res => {
							if (res.data) {
								this.listLoading = false;
								this.total = res.data.total;
								this.shops = res.data.rows;
							}
						}).catch(res => {
					alert("系统错误！");
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/shop"+"/"+row.id).then((res) => {
						if (res.data) {
							this.listLoading = false;
							if (this.total>this.pageSize&&(this.total-1)%this.pageSize==0){
								this.page=this.total/this.pageSize;
							}
							this.getShop();
							//NProgress.done();
							this.$message({
								message: '删除成功',
								type: 'success'
							});

						} else {
							this.listLoading = false;
							this.$message({
								message: res.data.message,
								type: 'success'
							});
						}
					}).catch(() => {
						alert("系统错误！")
					});
				}).catch(()=>{

				})
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.title = "编辑";
				this.shopForm = Object.assign({}, row);//回显
				// Object.assign({}, row);复制一份row的数据到{}空对象中
				this.shopFormVisible = true;//显示表单
			},
			//显示新增界面
			handleAdd: function () {
				this.title = "添加";
				this.shopFormVisible = true;
				this.shopForm = {
					id: null,
					name: '',
					tel: '',
					state: 1,
					registerTime: '',
					logo:'',
					address:'',
					admin:{}
				};
			},
			//保存
			editSubmit: function () {
				this.$refs.shopForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.shopForm);
							this.$http.put("/shop",para).then((res) => {
								this.editLoading = false;
								if (res.data){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['shopForm'].resetFields();
									this.shopFormVisible = false;
									if (this.id==null&&this.total>=this.pageSize){
										this.total=this.total+1;
										this.page=this.total/this.pageSize+1;
									}
									this.getShop();
								}else {
									this.$message({
										message: res.data.message,
										type: 'success'
									});
								}
							}).catch(res=>{
								this.editLoading = false;
								this.shopFormVisible = false;
								alert("系统错误！")
							});
						});
					}
				});
			},
			//获取选中的行
			selsChange: function (sels) {
				this.sels = sels;



			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);

				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();

					this.$http.patch("/shop", ids).then(res => {
						this.listLoading = false;
						if (res.data.message) {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						} else {
							this.$message({
								message: res.data.message,
								type: 'success'
							});
						}
						this.page = 1;
						this.getShop();
					}).catch(() => {
						alert("系统错误！")
					});
				})
			},
			//获取店铺信息
			getEmployees(){
				this.$http.get("/employee").then(res=>{
					if (res){
						this.admins=res.data;
					}
				})
			}
		},
		mounted() {
			this.getShop();
			this.getEmployees();
		}

	}
</script>

<style scoped>

</style>